import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { useUserStore } from '@vben/stores';
import { beginOfDay, endOfDay, formatDateTime, handleTree } from '@vben/utils';

import { getSimpleDeptList } from '#/api/system/dept';
import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales';

const userStore = useUserStore();

export function useCustomerSummaryTabs() {
  return [
    {
      tab: $t('views.crm.statistics.portrait.tabs.area'),
      key: 'area',
    },
    {
      tab: $t('views.crm.statistics.portrait.tabs.level'),
      key: 'level',
    },
    {
      tab: $t('views.crm.statistics.portrait.tabs.source'),
      key: 'source',
    },
    {
      tab: $t('views.crm.statistics.portrait.tabs.industry'),
      key: 'industry',
    },
  ]
} 

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'times',
      label: $t('views.crm.statistics.common.times'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        format: 'YYYY-MM-DD',
        picker: 'year',
      },
      defaultValue: [
        formatDateTime(beginOfDay(new Date(Date.now() - 3600 * 1000 * 24 * 7))),
        formatDateTime(endOfDay(new Date(Date.now() - 3600 * 1000 * 24))),
      ] as [Date, Date],
    },
    {
      fieldName: 'deptId',
      label: $t('views.crm.statistics.common.dept'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getSimpleDeptList();
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        treeDefaultExpandAll: true,
      },
      defaultValue: userStore.userInfo?.deptId,
    },
    {
      fieldName: 'userId',
      label: $t('views.crm.statistics.common.user'),
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
        placeholder: $t('views.crm.statistics.common.userPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(
  activeTabName: any,
): VxeTableGridOptions['columns'] {
  switch (activeTabName) {
    case 'industry': {
      return [
        {
          type: 'seq',
          title: $t('views.common.seq'),
        },
        {
          field: 'industryId',
          title: $t('views.crm.common.industryId'),
          minWidth: 100,
          cellRender: {
            name: 'CellDict',
            props: { type: DICT_TYPE.CRM_CUSTOMER_INDUSTRY },
          },
        },
        {
          field: 'customerCount',
          title: $t('views.crm.statistics.portrait.common.customerCount'),
          minWidth: 200,
        },
        {
          field: 'dealCount',
          title: $t('views.crm.statistics.portrait.common.dealCount'),
          minWidth: 200,
        },
        {
          field: 'industryPortion',
          title: $t('views.crm.statistics.portrait.common.industryPortion'),
          minWidth: 200,
        },
        {
          field: 'dealPortion',
          title: $t('views.crm.statistics.portrait.common.dealPortion'),
          minWidth: 200,
        },
      ];
    }
    case 'level': {
      return [
        {
          type: 'seq',
          title: $t('views.common.seq'),
        },
        {
          field: 'level',
          title: $t('views.crm.common.level'),
          minWidth: 100,
          cellRender: {
            name: 'CellDict',
            props: { type: DICT_TYPE.CRM_CUSTOMER_LEVEL },
          },
        },
        {
          field: 'customerCount',
          title: $t('views.crm.statistics.portrait.common.customerCount'),
          minWidth: 200,
        },
        {
          field: 'dealCount',
          title: $t('views.crm.statistics.portrait.common.dealCount'),
          minWidth: 200,
        },
        {
          field: 'industryPortion',
          title: $t('views.crm.statistics.portrait.common.industryPortion'),
          minWidth: 200,
        },
        {
          field: 'dealPortion',
          title: $t('views.crm.statistics.portrait.common.dealPortion'),
          minWidth: 200,
        },
      ];
    }
    case 'source': {
      return [
        {
          type: 'seq',
          title: $t('views.common.seq'),
        },
        {
          field: 'source',
          title: $t('views.crm.common.source'),
          minWidth: 100,
          cellRender: {
            name: 'CellDict',
            props: { type: DICT_TYPE.CRM_CUSTOMER_SOURCE },
          },
        },
        {
          field: 'customerCount',
          title: $t('views.crm.statistics.portrait.common.customerCount'),
          minWidth: 200,
        },
        {
          field: 'dealCount',
          title: $t('views.crm.statistics.portrait.common.dealCount'),
          minWidth: 200,
        },
        {
          field: 'industryPortion',
          title: $t('views.crm.statistics.portrait.common.industryPortion'),
          minWidth: 200,
        },
        {
          field: 'dealPortion',
          title: $t('views.crm.statistics.portrait.common.dealPortion'),
          minWidth: 200,
        },
      ];
    }
    default: {
      return [];
    }
  }
}
